// stores/app.js
import { defineStore } from 'pinia'
import { useCache } from '@/hooks/useCache'

const { wsCache } = useCache('localStorage')

export const useAppStore = defineStore('app', {
  state: () => {
    return {
      isCollapse: false, //菜单是否展开
      breadCrumbList: wsCache.get('breadCrumbList') ? wsCache.get('breadCrumbList') : []
    }
  },
  actions: {
    // 设置菜单合并
    setCollapse(e) {
      this.isCollapse = e
    },
    // 设置面包屑路由展示
    setBreadCrumbList(data, action) {
      if (action === 'route') {
        const newData = [...this.breadCrumbList]
        const filterData = newData
          .map((item) => {
            if (item.type !== 'route') {
              return { ...item }
            }
          })
          .filter((item) => item !== undefined)
        this.breadCrumbList = [data, ...filterData]
      } else {
        this.breadCrumbList.push(data)
      }
      wsCache.set('breadCrumbList', this.breadCrumbList)
    }
  }
})
